// 发送
const postMessage = function (dom, data = {
  type: ''
}) {
  if (data.type)
    dom.contentWindow.postMessage(data, '*')
}

// 自定义事件
const postEvent = {
  // map 鼠标缩放
  postMessage_mouse: new CustomEvent("postMessage_mouse"),
  // map 地图点击
  postMessage_click: new CustomEvent("postMessage_click"),
  // 获取小区基础信息
  postMessage_community_form: new CustomEvent("postMessage_community_form")
}
// 获取 和 触发
window.addEventListener('message', function (e) {
  if (e.data) {
    var events = ''
    switch (e.data.type) {
      case 'mouse':
        events = 'postMessage_mouse'
        break;
      case 'click':
        events = 'postMessage_click'
        break;
      case 'community_form':
        events = 'postMessage_community_form'
        break;
    }
    if(!events) return
    postEvent[events].data = e.data
    window.dispatchEvent(postEvent[events])
  }
})

export {
  postMessage
}